{% extends '_base_list.html' %}

{% block title %}
    系统用户
{% endblock %}


{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">
{% endblock %}


<!-- Content head -->
{% block head_title %}
    系统管理
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">资产管理</a></li>
        <li class="active">系统用户</li>
    </ol>
{% endblock %}

{% block tip %}
    <div class="alert alert-info mar-no">
        <i class="fa fa-circle"></i>&nbsp;&nbsp;系统用户是跳转登录资产时使用的用户，系统用户创建时，如果选择了自动推送，系统将会使用Ansible自动推送系统用户到资产中，如果资产不支持Ansible，请手动填写账号密码。
    </div>
{% endblock %}

<!-- Content body -->
{% block title_filter %}
    <div class="filter">
        <div class="row">
            <div class="col-xs-12 col-sm-2">
                <div class="form-group">
                    <input type="text" class="form-control" name="create_time" placeholder="请选择日期日期" autocomplete="off">
                </div>
            </div>

            <div class="col-xs-12 col-sm-3 pull-right">
                <div class="form-group">
                    <div class="input-group">
                        <input type="text" name="search" placeholder="请输入系统用户的名称" class="form-control">
                        <span class="input-group-btn">
                            <button name="search" class="btn btn-mint" type="button">
                                <i class="fa fa-search"></i>
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block body_operator %}
    <div class="operator mar-btm">
        <div class="row">
            <div class="col-xs-12 text-center">
                {% if current_user.has_perm('assets-systemuser-create') %}
                    <button class="btn btn-primary btn-sm" id="add"><i class=""></i> 新增用户</button>
                {% endif %}
                {% if current_user.has_perm('assets-systemuser-update') %}
                    <button class="btn btn-mint btn-sm" id="edit"><i class=""></i> 修改用户</button>
                {% endif %}
                {% if current_user.has_perm('assets-systemuser-delete') %}
                    <button class="btn btn-danger btn-sm" id="del"><i class=""></i> 删除用户</button>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block body_table %}
    <table id="adminuserTable" class="table table-auto"></table>
{% endblock %}


{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">

        $(function () {
            bees.initDaterangepicker($('input[name="create_time"]'),{
                opens: "right",
                timePicker: false,
                timePickerSeconds: false,
                singleDatePicker: false,
                locale: {
                    format: 'YYYY-MM-DD'
                }
            });

            // 回填和初始化默认过滤参数
            var createTimeEle = $("input[name='create_time']");
            var searchEle = $('input[name="search"]');

            var filterParams = JSON.parse(window.sessionStorage.getItem("filterParams"));
            if(filterParams){
                // 回填表单过滤条件
                createTimeEle.val(filterParams.create_time);
                searchEle.val(filterParams.search);
                window.sessionStorage.removeItem("filterParams");
            }else {
                // 默认表单过滤条件
                filterParams = {
                    'create_time': createTimeEle.val(),
                    'search': searchEle.val()
                };
            }
            $.fn.beesTable.defaultFilterParams(filterParams);

            // 初始化表格
            var $tableEle = $("#adminuserTable");
            var options = {
                columns: [
                    {title: '选择', field: 'selected', radio: true},
                    {title: '名称', field: 'name', sortable: false},
                    {title: '用户名', field: 'username', sortable: false},
                    {title: '资产', field: 'asset_count', sortable: false},
                    {title: '登录模式', field: 'login_mode', sortable: false,
                        formatter: function (value, row, index, field) {
                            // value: 字段值; row: 行记录数据; index: 行索引 ;field: 行字段
                            if(value === 1){
                                return '手动登录'
                            }
                            else if(value === 2){
                                return '自动登录 <span style="cursor: pointer;" class="fa fa-info-circle" data-toggle="tooltip" data-placement="top" title="密码登录">'
                            }
                            else if(value === 3){
                                return '自动登录 <span style="cursor: pointer;" class="fa fa-info-circle" data-toggle="tooltip" data-placement="top" title="秘钥登录"></span>'
                            }
                            else if(value === 4){
                                return '自动登录 <span style="cursor: pointer;" class="fa fa-info-circle" data-toggle="tooltip" data-placement="top" title="密码或秘钥登录"></span>'
                            }
                        }
                    },
                    {title: '优先级', field: 'priority', sortable: false},
                    {title: '自动推送', field: 'auto_push', sortable: false,
                        formatter: function (value, row, index, field) {
                            // value: 字段值; row: 行记录数据; index: 行索引 ;field: 行字段
                            if(value === true){
                                return '<span class="text text-success text-bold">是</span>'
                            }else{
                                return '<span class="text text-danger text-bold">否</span>'
                            }
                        }
                    },
                    {title: 'Sudo权限', field: 'sudo', sortable: false},
                    {title: 'Shell', field: 'shell', sortable: false},
                    {title: '创建时间', field: 'create_time', sortable: true}
                ],
                url:'{{ url_for("assets.api-systemuser-list") }}',
                sortName: 'create_time',
                sortOrder: 'desc',
                onLoadSuccess: function () {
                    // 数据加载完成后初始化tooltip
                    $('[data-toggle="tooltip"]').tooltip();
                }
            };
            var table = $tableEle.beesTable(options);


            // 搜索
            bees.bindTableSearch({
                table: $tableEle,
                searches: [
                    {eventType:'change', eventSource:createTimeEle, data:function () {
                            return {'create_time': createTimeEle.val()};
                        }
                    },
                    {eventType:'click', eventSource:$('button[name="search"]'), data: function () {
                            return {'search': searchEle.val()}
                        }
                    }
                ]
            });

            // 操作
            bees.bindTableOperator({
                table: $tableEle,
                operator: {
                    add: {
                        href: '{{ url_for('assets.systemuser-create') }}'
                    },
                    show: {
                        disable: true
                    },
                    edit: {
                        href: '{{ url_for('assets.systemuser-update', pk=default_pk) }}'
                    },
                    del: {
                        confirmCallback: function (data) {
                            setAjaxCSRFToken();
                            $.ajax({
                                type: 'post',
                                url: '{{ url_for("assets.systemuser-delete", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                                data : {},
                                success: function (result) {
                                    if (result.code != 200) {
                                        bees.tipError({
                                            message: result.data.message
                                        });
                                    }else {
                                       bees.tipSuccess({
                                            message: "删除成功"
                                        });
                                    }

                                    //reload
                                    table.bt.bootstrapTable('refresh');
                                }
                            })
                        }
                    }
                }
            })

        })
    </script>
{% endblock %}
